package com.example.demo.Controller;

import com.example.demo.Bean.Shop;
import com.example.demo.Bean.User;
import com.example.demo.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Boolean login(String username,String password){
        User userByName = userService.getUserByName(username);
        try{
            String pwd = userByName.getPassword();
            String md5 = username + password;
            byte[] bytes = md5.getBytes();
            String pwdmd = DigestUtils.md5DigestAsHex(bytes);
            if (pwd.equals(pwdmd)){
                return true;
            }
        }catch (Exception e){
            return false;
        }
        return false;
    }

    @PostMapping("/shoplogin")
    public Boolean shoplogin(String name,String password){
        Shop shopByName = userService.getShopByName(name);
        try{
            String pwd = shopByName.getPassword();
            String md5 = name + password;
            byte[] bytes = md5.getBytes();
            String pwdmd = DigestUtils.md5DigestAsHex(bytes);
            if (pwd.equals(pwdmd)){
                return true;
            }
        }catch (Exception e){
            return false;
        }
        return false;
    }
}
